package server.layer;

import java.sql.*;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 22.04.12
 * Time: 18:43
 * To change this template use File | Settings | File Templates.
 */
public class PersonDao implements iPersonDao {
    private static Connection conn = JDBCUtil.getConn();

    @Override
    public Person getPerson(int id) throws SQLException {
        Statement stat = JDBCUtil.getStat();
        Person person = null;
        ResultSet result = stat.executeQuery("SELECT * FROM  person WHERE person_id = " + id);

        while (result.next()) {
            person = new Person(result.getInt("person_id"), result.getString("fname"), result.getString("lname"),
                    result.getString("email"), result.getDate("birthdate"), result.getString("filename"),
                    result.getString("description"), result.getString("imgname"));
            person.setAddress(getAddress(person.getId()));
            person.setPhoneNumbers(getPhones(person.getId()));
        }
        return person;
    }

    @Override
    public void addPerson(Person person) throws SQLException {
        String insertPerson = "INSERT INTO person (person_id) VALUES (null)";
        PreparedStatement stat = conn.prepareStatement(insertPerson);
        stat.executeUpdate();
        stat.close();
    }

    @Override
    public void updatePerson(int id, Person person) throws SQLException {
        String updatePerson = "UPDATE person SET fname = ?, SET lname = ?  WHERE person_id = ?";
        PreparedStatement stat = conn.prepareStatement(updatePerson);
        stat.setString(1, person.getFirstName());
        stat.setString(2, person.getLastName());
        stat.setInt(3, person.getId());
        stat.executeUpdate();
        stat.close();
    }

    @Override
    public void deletePerson(int id) throws SQLException {

        Statement stat = JDBCUtil.getStat();
        stat.executeUpdate("DELETE * WHERE person_id = " + id);
        stat.close();
    }

    public String[] getPhones(int id) {
        return new String[0];
    }

    public String[] getAddress(int id) {
        return new String[0];
    }

    public byte[] getImage(int id)throws SQLException{
        String getImage = "SELECT image from person WHERE person_id = ?";
        PreparedStatement stat = conn.prepareStatement(getImage);
        stat.setInt(1, id);
        stat.execute();
        ResultSet res = stat.getResultSet();
        byte[] img = null ;
        while (res.next()){
            img = res.getBytes(1);
        }
        stat.close();
        return img;
    }

    public void setImage(byte[] image, int id) throws SQLException {
        String updImage = "UPDATE person SET image = ? WHERE person_id = ?";
        PreparedStatement stat = conn.prepareStatement(updImage);
        stat.setBytes(1, image);
        stat.setInt(2, id);
        stat.executeUpdate();
        stat.close();
    }

    public void getAll(ArrayList<Person> persons) throws SQLException {

        Statement stat = JDBCUtil.getStat();
        ResultSet result = stat.executeQuery("SELECT * FROM  person");

        while (result.next()) {
            Person person = new Person(
                    result.getInt("person_id"),
                    result.getString("fname"),
                    result.getString("lname"),
                    result.getString("email"),
                    result.getDate("birthdate"),
                    result.getString("filename"),
                    result.getString("description"),
                    result.getString("imgname"));

            person.setAddress(getAddress(person.getId()));
            person.setPhoneNumbers(getPhones(person.getId()));
            persons.add(person);
        }
    }
}
